package com.qb.plugin.okhttp;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
 * File: null.java
 * Creator: dingpwen
 * Company: shiwei
 * Created on: 2021/7/7 10:20
 * Email: dingpwen@gmail.com
 * Details: TODO
 */
public class OkHttpClassVisitor extends ClassVisitor {
    private String className;
    private boolean weaveEventListener;

    public OkHttpClassVisitor(ClassVisitor classVisitor, boolean weaveEventListener) {
        super(Opcodes.ASM7, classVisitor);
        this.weaveEventListener = weaveEventListener;
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        super.visit(version, access, name, signature, superName, interfaces);
        this.className = name;
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        //return super.visitMethod(access, name, descriptor, signature, exceptions);
        MethodVisitor mv = cv.visitMethod(access, name, descriptor, signature, exceptions);
        if(className.equals("okhttp3/OkHttpClient$Builder") && name.equals("<init>")){
            return mv == null ? null : new OkHttpMethodVisitor(access, descriptor, mv, weaveEventListener);
        }
        return mv;
    }
}
